<template>
  <div class="app-container">
    <div class="filter-container">
      <el-input v-model="filters.userinfo" placeholder="使用者名称/手机号码" style="width: 200px;" class="filter-item" @keyup.enter.native="handleFilter" />
      <org-unit-select v-if="isAdmin" v-model="filters.orgUnitId" placeholder="请选择店铺" :org-unit-type="1" />
      <el-date-picker
        v-model="selectedDate"
        type="daterange"
        align="right"
        unlink-panels
        range-separator="至"
        value-format="yyyy-MM-dd"
        start-placeholder="开始日期"
        end-placeholder="结束日期"
        :picker-options="pickerOptions"
      />
      <el-radio-group v-model="filters.status" style="margin: 0 3px;">
        <el-radio-button :label="0" @click.native.prevent="statusFilter(0)">未确认</el-radio-button>
        <el-radio-button :label="10" @click.native.prevent="statusFilter(10)">已确认</el-radio-button>
      </el-radio-group>
      <el-button v-waves class="filter-item" type="primary" icon="el-icon-search" @click="handleFilter">查询</el-button>
      <div style="margin-left: 3.125rem;">
        <el-checkbox v-for="col in dynamicColumn.couponUseRecords" v-model="col.show" class="filter-item" style="margin-left:10px;" @change="tableKey=tableKey+1">
          {{ col.name }}
        </el-checkbox>
      </div>
    </div>

    <el-table
      :key="tableKey"
      v-loading="listLoading"
      border
      fit
      :height="tableHeight"
      :data="list"
      highlight-current-row
      style="width: 100%;"
      @sort-change="sortChange"
      @selection-change="selsChange"
    >
      <el-table-column type="index" width="60" />
      <el-table-column prop="feeName" label="名称" min-width="160" />
      <el-table-column v-if="checkColumn('orgUnitName')" prop="orgUnitName" label="使用店铺" min-width="160" />
      <el-table-column prop="status" label="类型" width="70" :formatter="formatType" />
      <el-table-column prop="amount" label="抵扣额度" width="80" :formatter="formatAmount" />
      <el-table-column prop="feeAmount" label="费用" width="80" :formatter="formatFeeAmount" />
      <el-table-column v-if="checkColumn('carpoolTitle')" prop="carpoolTitle" label="拼车标题" min-width="160" />
      <el-table-column v-if="checkColumn('carpoolMoveName')" prop="carpoolMoveName" label="剧本名称" min-width="160" />
      <el-table-column v-if="checkColumn('carpoolStartTime')" prop="carpoolStartTime" label="预约时间" min-width="160" />
      <el-table-column v-if="checkColumn('time')" prop="time" label="优惠卷使用时间" min-width="160" />
      <el-table-column prop="couponUserNickname" label="优惠卷使用者" min-width="160" />
      <el-table-column v-if="checkColumn('couponUserPhoneNumber')" prop="couponUserPhoneNumber" label="优惠卷使用者手机号" min-width="160" />
    </el-table>

    <pagination v-show="total>0" :total="total" :page.sync="filters.page" :limit.sync="filters.limit" @pagination="loadPageList" />
  </div>
</template>

<script>
import util from '@/utils/util.js'
import { listCouponUseRecord } from '@/api/coupon-use-record'
import OrgUnitSelect from '../components/org-unit-select'
import waves from '@/directive/waves' // waves directive
import { mapGetters } from 'vuex'
import Pagination from '@/components/Pagination'

export default {
  name: 'Carpools',
  components: { Pagination, OrgUnitSelect },
  directives: { waves },
  data() {
    return {
      tableHeight: 260,
      tableKey: 0,
      list: null,
      total: 0,
      listLoading: true,
      sels: [], // 列表选中列
      filters: {
        page: 1,
        limit: 20,
        orgUnitId: null,
        userinfo: null,
        status: null
      },
      selectedDate: '',
      pickerOptions: {
        shortcuts: [{
          text: '最近一周',
          onClick(picker) {
            const end = new Date()
            const start = new Date()
            start.setTime(start.getTime() - 3600 * 1000 * 24 * 7)
            picker.$emit('pick', [start, end])
          }
        }, {
          text: '最近30天',
          onClick(picker) {
            const end = new Date()
            const start = new Date()
            start.setTime(start.getTime() - 3600 * 1000 * 24 * 30)
            picker.$emit('pick', [start, end])
          }
        }, {
          text: '最近90天',
          onClick(picker) {
            const end = new Date()
            const start = new Date()
            start.setTime(start.getTime() - 3600 * 1000 * 24 * 90)
            picker.$emit('pick', [start, end])
          }
        }]
      },
      orderBy: {
        orderColumn: '',
        order: ''
      }
    }
  },
  computed: {
    ...mapGetters(['userInfo', 'dynamicColumn'])
  },
  created() {
    this.tableHeight = window.innerHeight - 220
    this.loadPageList()
  },
  methods: {
    checkColumn(col) {
      if (this.dynamicColumn.couponUseRecords.hasOwnProperty(col)) {
        return this.dynamicColumn.couponUseRecords[col].show
      }
      return true
    },
    isAdmin() {
      const roles = this.userInfo.roles || []
      return roles.indexOf('Admin') !== -1
    },
    formatAmount(row) {
      return util.currency.format(row.amount) + '元'
    },
    formatFeeAmount(row) {
      return util.currency.format(row.feeAmount) + '元'
    },
    formatType(row) {
      if (row.status === 0) {
        return '未确认'
      } else if (row.status === 10) {
        return '已确认'
      } else {
        return '未知类型'
      }
    },
    loadPageList() {
      this.listLoading = true
      const param = { ...this.filters, ...this.orderBy }
      if (this.selectedDate && this.selectedDate.length >= 2) {
        param.start = this.selectedDate[0]
        param.end = this.selectedDate[1]
      }
      listCouponUseRecord(param).then(({ success, message, data }) => {
        this.listLoading = false
        if (success) {
          const { records, total } = data || {}
          this.list = records
          this.total = total
        } else {
          this.$message({
            message: message || '查询列表失败',
            type: 'error'
          })
        }
      }, () => {
        this.listLoading = false
      })
    },
    handleFilter() {
      this.filters.page = 1
      this.loadPageList()
    },
    statusFilter(status) {
      if (status === this.filters.status) {
        this.filters.status = null
      } else {
        this.filters.status = status
      }
      this.handleFilter()
    },
    sortChange({ prop, order }) {
      this.orderBy.orderColumn = prop
      this.orderBy.order = order
      this.handleFilter()
    },
    selsChange(sels) {
      this.sels = sels
    }
  }
}
</script>

<style lang="scss" scoped>
</style>
